package easymall.service;

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import easymall.dao.ProductsDao;
import easymall.po.Products;
import easymall.po.category;
import easymall.pojo.MyProducts;
@Service
public class ProductsServiceImpl implements ProductsService {
	@Autowired
	private ProductsDao productsDao;
	
	@Autowired
	private MongoTemplate mongoTemplate;
	
	@Override
	public List<String> allcategories() {
		
		// TODO Auto-generated method stub
				List<String> categories = new ArrayList<>();
				Query query = new Query();
				query.fields().include("name");
				String collectionName = "category";
				List<category> temps = mongoTemplate.find(query,category.class,collectionName);
				for (category item:temps){
					categories.add(item.getName());
				}
				
				return categories;
	}


	@Override
	public List<Products> prodlist(Map<String, Object> map) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
				Query query = new Query();
				Criteria criteria = new Criteria();
				String collectionName = "products";
				criteria.and("status").is(1);
				criteria.and("price").gte(map.get("minPrice")).lte(map.get("maxPrice"));
				System.out.println(map);
				if (map.get("name")!="" && map.get("name")!=null){
					criteria.and("name").regex("/"+map.get("name")+"/");
				}
				if (map.get("category")!="" && map.get("category")!=null){
					criteria.and("category").is(map.get("category"));
				}
				query.addCriteria(criteria);
				List<Products> products = mongoTemplate.find(query, Products.class,collectionName);
				System.out.println(products);
				return products;
	}

	
	@Override
	public Products prodInfo(String pid) {
		// TODO Auto-generated method stub
		Query query = new Query();
		query.addCriteria(Criteria.where("id").is(pid));
		List<Products> products = mongoTemplate.find(query,Products.class,"products");
		return products.get(0);
	}


	@Override
	public Products oneProduct(String product_id) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public String save(MyProducts myproducts, HttpServletRequest request) {
		String originName = myproducts.getImgurl().getOriginalFilename();
			
		String extName = originName.substring(originName.lastIndexOf("."));

		if (!(extName.equalsIgnoreCase(".jpg") || extName.equalsIgnoreCase(".png")
						|| extName.equalsIgnoreCase(".gif"))) {
			return "ͼƬ��׺���Ϸ�!";
		}
		try {
			BufferedImage bufImage = ImageIO.read(myproducts.getImgurl().getInputStream());
			bufImage.getHeight();
			bufImage.getWidth();
		} catch (Exception e) {
			return "���ļ�����ͼƬ��";
		}
		String dir = "";
		for (int i = 0; i < 8; i++) {
			dir += "/" + Integer.toHexString(new Random().nextInt(16));
		}
		
		String realpath = request.getServletContext().getRealPath("/WEB-INF");
		realpath += "/upload";
		// D:\java\Workspace\.metadata\.plugins\org.eclipse.wst.server.core
		// \tmp0\wtpwebapps\EasyMall18\WEB-INF/upload/2/6/2/7/2/d/2/1		
		System.out.println(realpath + dir+"************");
		// ƴ��ͼƬ�������ݿ��·��
		System.out.println(dir+"-------");	
		String imgurl = "/upload" + dir + "/" + originName;	
		System.out.println(imgurl);
		if (productsDao.findByImgurl(imgurl) != null) {
			String fname = imgurl.substring(0,imgurl.lastIndexOf("."));
			imgurl=fname+System.currentTimeMillis()+extName;
		}
		
		File file = new File(realpath + dir, originName);
		if (!file.exists()) {
			file.mkdirs();
		}
		try {
			myproducts.getImgurl().transferTo(file);
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		String id = UUID.randomUUID().toString();
		Products products = new Products();
		products.setId(id);
		products.setName(myproducts.getName());
		products.setCategory(myproducts.getCategory());
		products.setPrice(myproducts.getPrice());
		products.setPnum(myproducts.getPnum());
		products.setImgurl(imgurl);
		products.setDescription(myproducts.getDescription());
		productsDao.save(products);	
		return "商品添加成功";
	}


//	@Override
//	public void delprod(String id) {
//		// TODO Auto-generated method stub
//		productsDao.delprod(id);
//	}


	@Override
	public List<Products> allprods() {
		// TODO Auto-generated method stub
		Query query = new Query();
		List<Products> products = mongoTemplate.find(query,Products.class,"products");
		System.out.println(products);
		return products;
	}


	@Override
	public void updateSaleStatus(Map<String, Object> map) {
		// TODO Auto-generated method stub
		productsDao.updateSaleStatus(map);
	} 


	
}
